<template>
  <PageWrapper dense contentFullHeight fixedHeight contentClass="flex">
    <div class="w-1/4 xl:w-1/5 left-box">
      <BasicTree
        title="类别"
        search
        toolbar
        defaultExpandAll
        :selectedKeys="selectedKeys"
        :treeData="treeData"
        @select="handleSelect"
      />
    </div>
    <BasicTable @register="registerTable" class="w-3/4 xl:w-4/5">
      <template #toolbar>
        <a-button type="primary" @click="handleCreate">{{ t('新增') }}</a-button>
      </template>
      <template #bodyCell="{ column, record }">
        <template v-if="column.dataIndex == 'type'">
          <span :style="getType('style', record.type)">{{ getType('text', record.type) }}</span>
        </template>
        <template v-if="column.dataIndex == 'action'">
          <TableAction
            :actions="[
              {
                icon: 'clarity:note-edit-line',
                onClick: handleEdit.bind(null, record),
              },
              {
                icon: 'ant-design:delete-outlined',
                color: 'error',
                onClick: handleDelete.bind(null, record),
              },
            ]"
          />
        </template>
      </template>
    </BasicTable>
    <ManagementModal @register="registerModal" @success="reload" />
  </PageWrapper>
</template>
<script lang="ts" setup>
  import { ref, createVNode } from 'vue';
  import { BasicTable, useTable, TableAction, FormSchema, BasicColumn } from '/@/components/Table';
  import { PageWrapper } from '/@/components/Page';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { BasicTree, TreeItem } from '/@/components/Tree';
  import { Modal } from 'ant-design-vue';
  import { ExclamationCircleOutlined } from '@ant-design/icons-vue';
  import { getStoreManagementPageList, deleteStoreManagement } from '/@/api/erp/store/management';
  import { useModal } from '/@/components/Modal';
  import ManagementModal from './components/ManagementModal.vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  const { t } = useI18n();
  const searchFormSchema: FormSchema[] = [
    {
      field: 'keyword',
      label: '关键字',
      component: 'Input',
      colProps: { span: 8 },
      componentProps: {
        placeholder: '请输入单据编号或名称',
      },
    },
  ];

  const columns: BasicColumn[] = [
    {
      title: '单据编号',
      dataIndex: 'code',
      sorter: true,
    },
    {
      title: '单据名称',
      dataIndex: 'name',
      sorter: true,
    },
    {
      title: '单据类型',
      dataIndex: 'type',
      sorter: true,
    },
    {
      title: '时间',
      dataIndex: 'createDate',
      sorter: true,
    },
    {
      title: '经手人员',
      dataIndex: 'personNames',
    },
    {
      title: t('备注'),
      dataIndex: 'remark',
      sorter: true,
    },
  ];

  const treeData = ref<TreeItem[]>([
    {
      title: '全部',
      key: 'all',
      children: [
        {
          title: '入库单',
          key: 1,
        },
        {
          title: '出库单',
          key: 0,
        },
      ],
    },
  ]);

  const { notification } = useMessage();

  const type = ref('');
  const selectedKeys = ref<string[]>(['all']);

  const [registerModal, { openModal }] = useModal();
  const [registerTable, { reload }] = useTable({
    title: '仓库管理',
    api: getStoreManagementPageList,
    rowKey: 'id',
    columns,
    formConfig: {
      rowProps: {
        gutter: 16,
      },
      schemas: searchFormSchema,
    },
    beforeFetch: (params) => {
      return { ...params, type: type.value };
    },
    useSearchForm: true,
    showTableSetting: true,
    striped: false,
    actionColumn: {
      width: 80,
      title: t('操作'),
      dataIndex: 'action',
      slots: { customRender: 'action' },
    },
  });

  const getType = (type, record) => {
    if (type === 'style') {
      return { color: record === 1 ? '#5e95ff' : '#f59a23' };
    } else {
      return record === 1 ? '入库单' : '出库单';
    }
  };

  const handleCreate = () => {
    openModal(true, {
      isUpdate: false,
    });
  };
  0;
  const handleEdit = (record: Recordable) => {
    openModal(true, {
      id: record.id,
      isUpdate: true,
    });
  };

  const handleDelete = (record) => {
    Modal.confirm({
      title: t('提示信息'),
      icon: createVNode(ExclamationCircleOutlined),
      content: t('是否确认删除？'),
      okText: t('确认'),
      cancelText: t('取消'),
      async onOk() {
        await deleteStoreManagement(record.id);
        notification.success({
          message: t('提示'),
          description: t('删除成功'),
        });
        reload();
      },
      onCancel() {},
    });
  };

  const handleSelect = (typeKey) => {
    type.value = typeKey[0] === 'all' ? '' : typeKey[0];
    reload();
  };
</script>
<style lang="less" scoped>
  .left-box {
    margin-right: 8px;
    overflow: hidden;
    background: #fff;
    height: 100%;
  }
</style>
